package sjsu.android;


import java.sql.Date;

import dbAdapter.CreateFoodData;
import dbAdapter.FoodDBAdapter;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
public class SearchActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.search);

		CreateFoodData nutData = new CreateFoodData();
		nutData.create(this);

		// Intent intent = new Intent(this, DailyReportDatePickerActivity.class);
		//Intent intent = new Intent(this, MonthlyReportDatePickerActivity.class);
		//intent.putExtra("Date", new Date(110, 8, 30).toString());
		//startActivity(intent);

		final Button button = (Button) findViewById(R.id.SearchButton);
		FoodDBAdapter nutritionDbAdapter = new FoodDBAdapter(SearchActivity.this);
		nutritionDbAdapter.open();
		String[] foodNames = nutritionDbAdapter.retrieveAllFoodNamesAsStringArray();		
		nutritionDbAdapter.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.search_suggestion_list_item, foodNames);

		final AutoCompleteTextView editText = (AutoCompleteTextView) findViewById(R.id.EditText01);
		editText.setAdapter(adapter);
		editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_NULL) {
					button.performClick();
				}
				return false;
			}
		});

		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Perform action on click
				final ListView listView = (ListView) findViewById(R.id.ListView01);
				//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(HelloAndroid.this, R.layout.list_items, new String[] {"INdia", "China"});
				//listView.setAdapter(arrayAdapter);
				String query = editText.getText().toString();
				Log.v("HelloAndroid", "SearchQuery for " + query);


				final FoodDBAdapter nutritionDbAdapter = new FoodDBAdapter(SearchActivity.this);
				nutritionDbAdapter.open();
				Cursor cursor = nutritionDbAdapter.fetchNutritionWithLike(query);
				if (cursor.getCount() == 0) {
					TextView txt = (TextView) findViewById(R.id.TextViewMessage);
					txt.setText("No Results Found");

				} else if (cursor.getCount() == 1) {
					cursor.moveToFirst();
					Intent intent = new Intent(SearchActivity.this, NutritionInfo.class);
					intent.putExtra("itemName", cursor.getString(cursor.getColumnIndex(FoodDBAdapter.KEY_NAME)));
					startActivity(intent);
				} 
				else {
					TextView txt = (TextView) findViewById(R.id.TextViewMessage);
					txt.setText("");

					SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(SearchActivity.this,
							R.layout.list_items, cursor, 
							new String[] {FoodDBAdapter.KEY_NAME},
							new int[] {R.id.TextViewNutritionName});
					listView.setAdapter(simpleCursorAdapter);
					//cursor.close();
					nutritionDbAdapter.close();
					listView.setOnItemClickListener(new OnItemClickListener() {

						public void onItemClick(AdapterView<?> arg0, View arg1,
								int position, long rowId) {

							Cursor c = (Cursor) listView.getItemAtPosition(position);
							Log.v("HelloAndroid", "" + c.getString(c.getColumnIndex(FoodDBAdapter.KEY_NAME)));
							String foodName = c.getString(c.getColumnIndex(FoodDBAdapter.KEY_NAME));
							Intent intent = new Intent(SearchActivity.this, NutritionInfo.class);
							intent.putExtra("itemName", foodName);
							startActivity(intent);
						}

					});
				}
			}
		});
	}
}